package com.m.demo.mapper;

import com.m.demo.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {


    /**
     * 结果映射
     * 方法一：对字段进行重命名
     * @return
     */
    @Select("select id, username, password, age, gender, phone," +
            " delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            " from user_info")
//    @Select("select * from user_info")
    List<UserInfo> selectAll();

    /**
     * 结果映射
     * 方法二：使用注解
     * @return
     */
    @Results(id = "BaseMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from user_info")
    List<UserInfo> selectAll2();

    @Select("select * from user_info;")
    List<UserInfo> selectAll3();

    @ResultMap(value = "BaseMap")
//    @Select("select * from user_info where id = #{id}")
    @Select("select * from user_info where id = ${id}")
    UserInfo selectOne(Integer id);

    @Select("select * from user_info where username = ${username}")
    List<UserInfo> selectByName(String username);

    @Select("select * from user_info where id = #{userId}")
    UserInfo selectOne2(@Param("userId") Integer userId);

    //返回自增ID
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, password, age, gender,phone) " +
            "values (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    //对Insert的参数进行重命名
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, password, age, gender, phone) " +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);

    @Update("update user_info set age = #{age} where id = #{id}")
    Integer update(UserInfo userInfo);

    @Select("select * from user_info order by id ${sort}")
    List<UserInfo> selectUserBySort(String sort);

//    @Select("select * from user_info where username like '${username}'")
    @Select("select * from user_info where username like concat('%', #{username}, '%')")
    List<UserInfo> selectUserByLike(String username);

}
